package com.shqcjd.sinopecdemomobile.ui.activity.count;

import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.AbsoluteSizeSpan;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.shqcjd.sinopecdemomobile.R;
import com.shqcjd.sinopecdemomobile.ui.base.BaseActivity;
import com.shqcjd.sinopecdemomobile.ui.base.BasePresenter;
import com.shqcjd.sinopecdemomobile.util.ToastUtils;
import com.shqcjd.sinopecdemomobile.widget.SaveDraftDialog;

import butterknife.BindView;

public class FormActivity extends BaseActivity {

    @BindView(R.id.ed_form_text1)
    EditText edFormText1;
    @BindView(R.id.ed_form_text9)
    EditText edFormText9;
    @BindView(R.id.ed_form_text10)
    EditText edFormText10;
    @BindView(R.id.ed_form_text2)
    EditText edFormText2;
    @BindView(R.id.ed_form_text13)
    EditText edFormText13;
    @BindView(R.id.ed_form_text14)
    EditText edFormText14;
    @BindView(R.id.ed_form_text4)
    EditText edFormText4;
    @BindView(R.id.ed_form_text15)
    EditText edFormText15;
    @BindView(R.id.ed_form_text16)
    EditText edFormText16;
    @BindView(R.id.ed_form_text3)
    EditText edFormText3;
    @BindView(R.id.ed_form_text11)
    EditText edFormText11;
    @BindView(R.id.ed_form_text12)
    EditText edFormText12;
    @BindView(R.id.ed_form_text5)
    EditText edFormText5;
    @BindView(R.id.ed_form_text6)
    EditText edFormText6;
    @BindView(R.id.ed_form_text7)
    EditText edFormText7;
    @BindView(R.id.ed_form_text8)
    EditText edFormText8;
    @BindView(R.id.ed_form_text17)
    EditText edFormText17;
    @BindView(R.id.ed_form_text18)
    EditText edFormText18;
    @BindView(R.id.ed_form_text19)
    EditText edFormText19;
    @BindView(R.id.tv_form_text18)
    TextView tvFormText18;
    @BindView(R.id.tv_form_text22)
    TextView tvFormText22;
    @BindView(R.id.tv_form_text19)
    TextView tvFormText19;
    @BindView(R.id.tv_form_text23)
    TextView tvFormText23;
    @BindView(R.id.tv_form_text20)
    TextView tvFormText20;
    @BindView(R.id.tv_form_text24)
    TextView tvFormText24;
    @BindView(R.id.tv_form_text21)
    TextView tvFormText21;
    @BindView(R.id.tv_form_text25)
    TextView tvFormText25;
    @BindView(R.id.tv_form_tip1)
    TextView tvFormTip1;
    @BindView(R.id.btn_form_clear)
    Button btnFormClear;
    @BindView(R.id.btn_form_count)
    Button btnFormCount;
    @BindView(R.id.btn_form_quit)
    Button btnFormQuit;
    @BindView(R.id.tv_form_tip0)
    TextView tvFormTip0;
    @BindView(R.id.btn_form_result_count)
    Button btnFormResultCount;
    @BindView(R.id.tv_form_result1)
    TextView tvFormResult1;
    @BindView(R.id.tv_form_result2)
    TextView tvFormResult2;
    @BindView(R.id.tv_form_result3)
    TextView tvFormResult3;
    @BindView(R.id.tv_form_result4)
    TextView tvFormResult4;
    @BindView(R.id.ed_form_name)
    EditText edFormName;
    @BindView(R.id.ed_form_group)
    EditText edFormGroup;

    private double z1, z2, x1, x2, x3, x4;
    private SaveDraftDialog saveDraftDialog;

    private String str1,str2,str3,str4,str5,str6,str7,str8,str9,str10,str11,str12,str13,str14,str15,str16,str17,str18,str19;
    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_form;
    }

    @Override
    public void initView() {
        super.initView();
        double d = 0.145;
        btnFormCount.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                countForm();
            }
        });
        btnFormQuit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        btnFormClear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clearViewData();
            }
        });
        tvFormTip0.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDialog("(1)主动轴指与打表工装连接的半联轴器所对应的机组名称;\n(2)从动轴指被打表的半联轴器所对应的机组名称");
            }
        });
        tvFormTip1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDialog("1、三表找正法。打表示意圆外记录径向打表数据，圆内记录端面打表数据。2、风机为调整对象，输入参数时注意，上张口、左张口输入正值，下张口、右张口输入负值;压缩机偏高、偏左输入负值，压缩机偏低、偏右输入正值。\n2、对于上下调节，数据为正表示抬高，反之降低；对于左右调节，数据为正，表示向左移动，反之向右(面向联轴器端面) ;程序中所有数据单位为mmo");
            }
        });
        btnFormResultCount.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                commitResult();
            }
        });
    }

    private Spannable getSpannable(String str, String s){
        Spannable sp = new SpannableString(str);
        sp.setSpan(new AbsoluteSizeSpan(8, true), str.indexOf(s), str.indexOf(s)+1, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        sp.setSpan(new AbsoluteSizeSpan(8, true), str.lastIndexOf(s), str.lastIndexOf(s)+1, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        return sp;
    }

    private void showDialog(String str){
        if(saveDraftDialog==null){
            saveDraftDialog = new SaveDraftDialog(this);
        }
        saveDraftDialog.show();
        saveDraftDialog.setViewText(str);
    }

    private String doubleFprmat(double d){
        String str = String.format("%.2f", d);
        return str;
    }

        private void countForm(){
            if(!edIsNull()){
                z1 = (strChangeDouble(str13)+strChangeDouble(str14))/2-(strChangeDouble(str9)+strChangeDouble(str10))/2;
                z2 = (strChangeDouble(str11)+strChangeDouble(str12))/2-(strChangeDouble(str15)+strChangeDouble(str16))/2;//0.7894736842105263

                x1 = (z1-strChangeDouble(str8))*strChangeDouble(str7)/strChangeDouble(str5);
                x2 = (z1-strChangeDouble(str8))*(strChangeDouble(str6)+strChangeDouble(str7))/strChangeDouble(str5);
                x3 = (z2-strChangeDouble(str17))*strChangeDouble(str7)/strChangeDouble(str5);
                x4 = (z2-strChangeDouble(str17))*(strChangeDouble(str6)+strChangeDouble(str7))/strChangeDouble(str5);
                tvFormText18.setText(doubleFprmat(x2-x1+(strChangeDouble(str2)-strChangeDouble(str1))/2-strChangeDouble(str18)));
                tvFormText19.setText(tvFormText18.getText());
                tvFormText20.setText(doubleFprmat(x2+(strChangeDouble(str2)-strChangeDouble(str1))/2-strChangeDouble(str18)));
                tvFormText21.setText(tvFormText20.getText());
                tvFormText22.setText(doubleFprmat(x4-x3+(strChangeDouble(str3)-strChangeDouble(str4))/2-strChangeDouble(str19)));
                tvFormText23.setText(tvFormText22.getText());
                tvFormText24.setText(doubleFprmat(x4+(strChangeDouble(str3)-strChangeDouble(str4))/2-strChangeDouble(str19)));
                tvFormText25.setText(tvFormText24.getText());
            }else{
                Toast.makeText(this, "请确认所有参数填写完毕", Toast.LENGTH_SHORT).show();
            }
        }

    private void commitResult(){
        String a = edFormGroup.getText().toString().trim();
        String b = edFormName.getText().toString().trim();
        if(a.equals("")||b.equals("")){
            ToastUtils.showShort(this, "请输入内容");
            return;
        }
        if(!edIsNulls()){
            double ychui = (strChangeDouble(str1) - strChangeDouble(str2))/2;
            if(ychui>0){
                tvFormResult1.setText(a+"比"+b+" 高"+doubleFprmat(ychui*0.01)+"mm("+(int)ychui+"道)");
            }else if(ychui==0){
                tvFormResult1.setText("高低 0 对 0");
            }else{
                tvFormResult1.setText(a+"比"+b+" 低"+doubleFprmat(Math.abs(ychui)*0.01)+"mm("+Math.abs((int)ychui)+"道)");
            }

            double yshui = (strChangeDouble(str4) - strChangeDouble(str3))/2;
            if(yshui>0){
                tvFormResult2.setText(a+"比"+b+" 偏左"+doubleFprmat(yshui*0.01)+"mm("+(int)yshui+"道)");
            }else if(yshui==0){
                tvFormResult2.setText("左右 0 对 0");
            }else{
                tvFormResult2.setText(a+"比"+b+" 偏右"+doubleFprmat(Math.abs(yshui)*0.01)+"mm("+Math.abs((int)yshui)+"道)");
            }

            double pchui = (strChangeDouble(str9)+strChangeDouble(str10))/2-(strChangeDouble(str13)+strChangeDouble(str14))/2;
            if(pchui>0){
                tvFormResult3.setText("下开口"+doubleFprmat(pchui*0.01)+"mm("+(int)pchui+"道)");
            }else if(pchui==0){
                tvFormResult3.setText("上下 0 开口");
            }else{
                tvFormResult3.setText("上开口"+doubleFprmat(Math.abs(pchui)*0.01)+"mm("+Math.abs((int)pchui)+"道)");
            }

            double pshui = (strChangeDouble(str15)+strChangeDouble(str16))/2-(strChangeDouble(str11)+strChangeDouble(str12))/2;
            if(pshui>0){
                tvFormResult4.setText("右开口"+doubleFprmat(pshui*0.01)+"mm("+(int)pshui+"道)");
            }else if(pshui==0){
                tvFormResult4.setText("左右 0 开口");
            }else{
                tvFormResult4.setText("左开口"+doubleFprmat(Math.abs(pshui)*0.01)+"mm("+Math.abs((int)pshui)+"道)");
            }
        }else{
            ToastUtils.showShort(this, "请确认所有参数填写完毕");
        }
    }

    private boolean edIsNull(){
        str1 = edFormText1.getText().toString().trim();
        str2 = edFormText2.getText().toString().trim();
        str3 = edFormText3.getText().toString().trim();
        str4 = edFormText4.getText().toString().trim();
        str5 = edFormText5.getText().toString().trim();
        str6 = edFormText6.getText().toString().trim();
        str7 = edFormText7.getText().toString().trim();
        str8 = edFormText8.getText().toString().trim();
        str9 = edFormText9.getText().toString().trim();
        str10 = edFormText10.getText().toString().trim();
        str11 = edFormText11.getText().toString().trim();
        str12 = edFormText12.getText().toString().trim();
        str13 = edFormText13.getText().toString().trim();
        str14 = edFormText14.getText().toString().trim();
        str15 = edFormText15.getText().toString().trim();
        str16 = edFormText16.getText().toString().trim();
        str17 = edFormText17.getText().toString().trim();
        str18 = edFormText18.getText().toString().trim();
        str19 = edFormText19.getText().toString().trim();
        if(str1.equals("")||str2.equals("")||str3.equals("")||str4.equals("")||str5.equals("")||
                str6.equals("")||str7.equals("")||str8.equals("")||str9.equals("")||str10.equals("")||
                str11.equals("")||str12.equals("")||str13.equals("")||str14.equals("")||str15.equals("")||
                str16.equals("")||str17.equals("")||str18.equals("")||str19.equals("")){
            return true;
        }
        return false;
    }

    private boolean edIsNulls(){
        str1 = edFormText1.getText().toString().trim();
        str2 = edFormText2.getText().toString().trim();
        str3 = edFormText3.getText().toString().trim();
        str4 = edFormText4.getText().toString().trim();
        str9 = edFormText9.getText().toString().trim();
        str10 = edFormText10.getText().toString().trim();
        str11 = edFormText11.getText().toString().trim();
        str12 = edFormText12.getText().toString().trim();
        str13 = edFormText13.getText().toString().trim();
        str14 = edFormText14.getText().toString().trim();
        str15 = edFormText15.getText().toString().trim();
        str16 = edFormText16.getText().toString().trim();
        if(str1.equals("")||str2.equals("")||str3.equals("")||str4.equals("")||str9.equals("")||str10.equals("")||
                str11.equals("")||str12.equals("")||str13.equals("")||str14.equals("")||str15.equals("")||
                str16.equals("")){
            return true;
        }
        return false;
    }

    private double strChangeDouble(String str){
        double d = 0;
        d = Double.parseDouble(str);
        return d;
    }

    private void clearViewData(){
        edFormText1.setText("");
        edFormText2.setText("");
        edFormText3.setText("");
        edFormText4.setText("");
        edFormText5.setText("");
        edFormText6.setText("");
        edFormText7.setText("");
        edFormText8.setText("");
        edFormText9.setText("");
        edFormText10.setText("");
        edFormText11.setText("");
        edFormText12.setText("");
        edFormText13.setText("");
        edFormText14.setText("");
        edFormText15.setText("");
        edFormText16.setText("");
        edFormText17.setText("");
        edFormText18.setText("");
        edFormText19.setText("");
        tvFormText18.setText("");
        tvFormText19.setText("");
        tvFormText20.setText("");
        tvFormText21.setText("");
        tvFormText22.setText("");
        tvFormText23.setText("");
        tvFormText24.setText("");
        tvFormText25.setText("");
        edFormGroup.setText("");
        edFormName.setText("");
        tvFormResult1.setText("");
        tvFormResult2.setText("");
        tvFormResult3.setText("");
        tvFormResult4.setText("");
    }
}
